<?php

namespace App\Models\Product;

use Illuminate\Database\Eloquent\Model;

use DateTimeInterface;

use App\Models\Config as ConfigModel;

class Product extends Model
{
    protected $table = 'product';

    protected $appends = [
        'thumb_text', // 单张
        'thumb_list', // 多张
    ];

    protected $guarded = [];

    public $timestamps = false;

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public function getThumbTextAttribute()
    {
        $url = ConfigModel::where('name', 'url')->value('value');

        $url = env('SITE_URL', $url);

        $thumb = !empty($this->attributes['thumbs']) ? explode(',', $this->attributes['thumbs']) : [];

        if (count($thumb) > 0) {
            $thumb = $url . $thumb[0];
        } else {
            $thumb = $url . '/assets/images/11.png';
        }

        return $thumb;
    }

    public function getThumbListAttribute()
    {
        $url = ConfigModel::where('name', 'url')->value('value');

        $url = env('SITE_URL', $url);

        $thumb = !empty($this->attributes['thumbs']) ? $this->attributes['thumbs'] : '';

        if (!empty($thumb)) {

            $thumb  = str_replace('/uploads/', $url . '/uploads/', $thumb);

            $thumb = explode(',', $thumb);

            return $thumb;
        } else {
            $thumb = [$url . '/assets/images/11.png'];

            return $thumb;
        }
    }


}
